<?php

namespace app\api\controller;
use app\common\logic\Logic;
use app\common\logic\MyCar as MyCarLogic;
use app\common\model\Car as CarModel;
use app\common\model\MyCar;
use think\Db;
use think\Exception;
use think\Log;

class Car extends User
{

    /**
     * 座驾列表
     */
    public function Cars()
    {
        $map['type'] = 1;
        $map['status'] = 1;
        $data  = (new CarModel())->getCar($map);
        api_return(1,'获取成功',$data);
    }

    /**
     * 我的座驾列表
     */
    public function MyCars()
    {
        $map['m.type'] = 1;
        $map['m.user_id'] = $this->user_id;
        $rows = (new MyCar())->getMyCar($map);
        
        api_return(1,'获取成功',$rows);
    }

    /**
     * 头饰列表
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function HeadWear()
    {
        $map['type']   = 2;
        $map['status'] = 1;
        $model = new CarModel();
        $rows = $model->headWear($map,$this->user_id);
        api_return(1,'获取成功',$rows);
    }
    
    
    /**
     * 装备坐骑
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function useCar(int $id)
    {
        $map['car_id']   = $id;
        $map['end_time'] = [['>',time()],['=',0],'or'];
        $map['user_id']  = $this->user_id;
        $map['status']   = 1;

        $data = MyCar::where($map)->find();
        if ($data){
            Db::startTrans();
            try{
                $where['user_id'] = $this->user_id;
                $where['is_use']  = 1;
                $where['status']  = 1;
                $where['type']    = $data['type'];

                MyCar::where($where)->update(['is_use'=>0]);
                $data->save(['is_use'=>1]);
            }catch (Exception $e){
                api_return(0,$e->getMessage());
            }
            api_return(1,'使用成功');
        }

        api_return(0,'参数错误');

    }

    /**
     * 卸下头饰
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function unload()
    {
        if (input('type') == 1){
            $where['type'] = 1;
        }else{
            $where['type'] = 2;
        }

        $result = MyCar::where($where)->update(['is_use'=>0]);
        if ($result !== false){
            api_return(1,'操作成功');
        }
        api_return(0,'操作失败');
    }


    
    /**
     * 座驾详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $id = input('post.id');
        if (!is_numeric($id)) api_return(0,'参数错误');

        $map['car_id'] = $id;
        $data = Db::name('car')
            ->where($map)
            ->field('car_id,thumbnail,price,car_name,status')
            ->cache(30)
            ->find();

        api_return(1,'获取成功',$data);
    }

    /**
     * 购买座驾
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function buyCar()
    {
        $this->ApiLimit(2,$this->user_id);

        $request = request()->only(['car_id','day'],'post');

        $where['car_id'] = $request['car_id'];
        $where['status']   = 1;

        $car = Db::name('car')->where($where)->find();
        if (!$car || $car['status'] ==0) api_return(0,'禁止购买');

        $data['car_id'] = $request['car_id'];
        $data['user_id']  = $this->user_id;
        $data['type']  = $car['type'];
        $model = new MyCarLogic();
        $myCar = $model->where($data)->find();

        $time = time();
        if ($myCar){
            //续费
            $data['my_id'] = $myCar['my_id'];
            if($myCar['status'] ==1)
                $time = $myCar['end_time'];
        }

        $data['end_time'] = strtotime('+'.$car['day'].' day', $time);
        $data['status']  = 1;
        $money = $car['price'];

        Db::startTrans();
        try{
            $this->accountDec($money);
            $result = $model->saveChange($data);
            Db::commit();
        }catch(Exception $e){
            Db::rollback();
            Log::error($e->getMessage());
            api_return(0,'购买失败');
        }

        if ($data['type'] == 1){
            $name = '坐骑';
        }else{
            $name = '头饰';
        }

        if ($result){
            money_detail(2,$this->user_id, '('.$car['car_name'].')'.$name.'购买',$money,'购买'.$name);
            api_return(1,'购买成功');
        }
        api_return(0,$model->getError());
    }
    
}